﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

using System.ComponentModel;

using System.Data.Linq;
using System.Data.Linq.Mapping;

using System.Linq.Expressions;
using System.Reflection;

/// <summary>
///ShoppingCartAccess 的摘要说明
/// </summary>
public class ShoppingCartAccess
{
    private static DataClassesDataContext da = new DataClassesDataContext();
	public ShoppingCartAccess()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    private static string shoppingCartId
    {
        get
        {
            // get the current HttpContext
            HttpContext context = HttpContext.Current;
            // try to retrieve the cart ID from the user cookie
            
            // if the cart ID isn't in the cookie...
            
                // check if the cart ID exists as a cookie
                if (context.Request.Cookies["BalloonShop_CartID"] != null)
                {
                    string cartId = context.Request.Cookies["BalloonShop_CartID"].Value;
                    // return the id
                    return cartId;
                }
                else
                // if the cart ID doesn't exist in the cookie as well, generate a new ID
                {

                    // generate a new GUID
                string    cartId = Guid.NewGuid().ToString();
                    // create the cookie object and set its value
                    HttpCookie cookie = new HttpCookie("BalloonShop_CartID", cartId);
                    // set the cookie's expiration date
                    int howManyDays = BalloonShopConfiguration.CartPersistDays;
                    DateTime currentDate = DateTime.Now;
                    TimeSpan timeSpan = new TimeSpan(howManyDays, 0, 0, 0);
                    DateTime expirationDate = currentDate.Add(timeSpan);
                    cookie.Expires = expirationDate;
                    // set the cookie on the client's browser
                    context.Response.Cookies.Add(cookie);
                    // return the CartID
                    return cartId.ToString();
                }
           
        }
    }
    public static bool AddItem(string productId, string attributes)
    {
        try
        {
            return (da.ShoppingCartAddItem(shoppingCartId, Int32.Parse(productId), attributes) != -1);
    
        }
        catch
        {
            // prevent the exception from propagating, but return false to
            // signal the error
            return false;
        }
    }
    public static bool UpdateItem(string productId, int quantity)
  {
      try
      {
          return (da.ShoppingCartUpdateItem(shoppingCartId, Int32.Parse(productId), quantity) != -1);

      }
      catch
      {
          // prevent the exception from propagating, but return false to
          // signal the error
          return false;
      }
  }
    public static bool RemoveItem(string productId)
    {
        try
        {
            return (da.ShoppingCartRemoveItem(shoppingCartId, Int32.Parse(productId)) != -1);

        }
        catch
        {
            // prevent the exception from propagating, but return false to
            // signal the error
            return false;
        }
    }
    // Retrieve shopping cart items
    public static ISingleResult<ShoppingCartGetItemsResult> GetItems()
    {
      return  da.ShoppingCartGetItems(shoppingCartId);
    }
    public static decimal GetTotalAmount()
    {
        string theo = da.ShoppingCartGetTotalAmount(shoppingCartId).FirstOrDefault().Column1.ToString();
        return Decimal.Parse(theo);
    }
    public static int CountOldCarts(byte days)
    {
        try
        {
            string cold = da.ShoppingCartCountOldCarts(0).FirstOrDefault().Column1.Value.ToString();
            return Byte.Parse(cold);
        }
        catch
           
        {
            return -1;
        }
    }
     public static bool DeleteOldCarts(byte days)
  {
      try
      {
          da.ShoppingCartDeleteOldCarts(days);
          return true;
      }
      catch
      {
          return false;
      }
  }
}